package edu.aut.bme.hu.orkomet.service;


import java.util.logging.Level;
import java.util.logging.Logger;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryException;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

import edu.aut.bme.hu.orkomet.Application;
import edu.aut.bme.hu.orkomet.engine.RDFEngine;

import javax.jws.WebService;

/**
 *
 * @author Administrator
 */
@WebService
public class Frontend {
    public String executeQuery(String sparqlString){
    	QueryExecution qexec = null;
        try {
        	Query query = QueryFactory.create(sparqlString) ;
            qexec = QueryExecutionFactory.create(query, RDFEngine.GetModel()) ;
            ResultSet results = qexec.execSelect();
            int delay=Application.getSettingsData().getCommon().getDelay();
            if(delay>0)
            	Thread.sleep(delay);
            return ResultSetFormatter.asXMLString(results);
        }catch(QueryException e){
      	  Logger.getLogger(Frontend.class.getName()).log(Level.WARNING, null, e);
    	  return e.getMessage();
        } catch(Exception e){
        	  Logger.getLogger(Frontend.class.getName()).log(Level.SEVERE, null, e);
        	  return "";
        } finally {
        	if(qexec!=null)
        		qexec.close();
        }
    }
}
